#ifndef TASK_H_
#define TASK_H_

#include <stdint.h>
#include <sys/epoll.h>

enum EventHandlerRetVal {
    RET_SYS_ERR = -1,
    RET_NORMAL = 0,
    RET_TASK_DELETED = 1,
};

struct context;
struct task;
/**
 * return:
 *     正常返回0， 致命错误返回-1，否者返回正数。
 */
typedef enum EventHandlerRetVal (*eventHandler)(const struct context *cxt,
        const struct epoll_event *e);

struct task {
    int fd;
    struct epoll_event watchEvents; /* 需要监视的事件 */
    eventHandler handler; /* 事件处理函数 */
};

struct context {
    int epollFd; /* epoll文件 */
    struct epoll_event *events; /* epoll事件缓存 */
    size_t size;
};

struct context *contextCreate(int size);
void contextRelease(struct context *ctx);
int contextAddTask(const struct context *ctx, struct task *task);
int contextDeleteTask(const struct context *ctx, struct task *task);
int contextTaskSetEvents(const struct context *ctx, struct task *task, uint32_t events);
int contextTaskClearEvents(const struct context *ctx, struct task *task, uint32_t events);
int contextTaskResetEvents(const struct context *ctx, struct task *task, uint32_t events);
int contextTaskChange(const struct context *ctx, struct task *task);

#endif /* TASK_H_ */
